/*
	klib_volume_manager.c

	contains volume manager related functionality

	Author: Aidan Goddard 30/1/13
*/

#include"headers/ktypes.h"
#include"headers/klib_volume_manager.h"

#include"../kernel/headers/scheduler_functions.h"
#include"../kernel/headers/bakery_lock.h"
#include"../kernel/headers/kernel_tables.h"


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// tables

// global file system driver table located at 0x4900000 (73MB) + KOFFSET - 256 entries
struct __attribute__((__packed__,aligned(256))) FS_LIST
{
	HANDLE 		handle;
};

// global storage driver table located at 0x4910000 (73MB + 64KB) + KOFFSET - 256 entries
struct __attribute__((__packed__,aligned(256))) ST_LIST
{
	HANDLE		handle;

	// 512 byte sector read/write function pointers
	uint32_t 	(*DiskRead512)(uint32_t p1, uint32_t p2, void *buffer, uint64_t sec_start, uint64_t sec_count);
	uint32_t 	(*DiskWrite512)(uint32_t p1, uint32_t p2, void *buffer, uint64_t sec_start, uint64_t sec_count);
	uint32_t 	(*Release)(void);

	// state control
	uint32_t 	is_releasing;
};

// global disk table located at 0x4810000 (72MB + 64k) + KOFFSET - 960 entries
struct __attribute__((__packed__,aligned(1024))) DISK_LIST
{
	HANDLE 		handle;		// handle of this disk
	uint64_t 	sectors;	// 512-byte sector count
	HANDLE		driver;		// driver handle
	uint32_t 	p1;			// param 1 and param 2 to be passed to the disk driver
	uint32_t 	p2;

	// other disk related info here to come later (probably)
};

// global volume table located at 0x4800000 (72MB) + KOFFSET - 256 entries
struct __attribute__((__packed__,aligned(256))) VOLUME_LIST
{
	uint32_t 	volume_id;		// is 0 for unused, 1 = A, 2 = B, etc
	uint32_t	reserved;
	HANDLE		disk;			// the handle of the disk it is located on
	uint64_t	sector_base;	// the sector start offset within the disk
	uint64_t 	sector_count;	// number of sectors in the volume
	HANDLE		file_system;	// handle of the filesystem driver for this volume (or 0 for no recognised filesystem)

	// this information is duplicated from other tables to increase performance
	HANDLE		disk_driver; 	// handle of the disk driver for the disk this volume is on (same as disk->driver)
	uint32_t	driver_p1;		// param 1 and param 2 to be passed to the disk driver
	uint32_t 	driver_p2;
};

// global pointers
static struct VOLUME_LIST *volumes = (struct VOLUME_LIST*)(KOFFSET + 0x4800000);
static struct DISK_LIST *disks = (struct DISK_LIST*)(KOFFSET + 0x4810000);
static struct FS_LIST *fss = (struct FS_LIST*)(KOFFSET + 0x4900000);
static struct ST_LIST *strgs = (struct ST_LIST*)(KOFFSET + 0x4910000);

// global variables
static volatile uint32_t active_volumes;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// functions to fetch volumes

// function to get the number of volumes in the system
uint32_t	__KLIB__GetVolumeCount(uint32_t *volume_count)
{
	// get the volume count
	*volume_count = active_volumes;

	// finished
	return VOLUME_MANAGER_SUCCESS;
}

// function to get a list of the current active volumes
// each volume returned as a number between 1 and 255 (0 is invalid). 1 represents drive A, 2 = B, 26 = Z, 27 = AA, 28 = AB, etc
uint32_t	__KLIB__GetVolumes(uint32_t *volumes, uint32_t max_volume_count)
{
	// finished
	return VOLUME_MANAGER_SUCCESS;
}






















